package cn.edu.scau.cmi.ema.controller;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.base.CmiEmaController;
import cn.edu.scau.cmi.ema.domain.Address;
import cn.edu.scau.cmi.ema.domain.Material;
import cn.edu.scau.cmi.ema.domain.Materialaddress;
import cn.edu.scau.cmi.ema.domain.Unit;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Set;

@Controller("MaterialaddressController")
public class MaterialaddressController extends CmiEmaController {

	@RequestMapping("/listMaterialaddresss")
	public ModelAndView listMaterialaddresss(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = materialaddressPaginationService.updatePagination(request, pagination);
		Set<Materialaddress> materialaddresss = materialaddressPaginationService
				.getPaginationMaterialaddresss(pagination);

		mav.addObject("materialaddresss", materialaddresss);
		mav.addObject("entityName", "materialaddress");
		mav.addObject("pagination", pagination);
		mav.setViewName("materialaddress/listMaterialaddresss");
		return mav;
	}

	public String listMaterialaddresss() {
		return "redirect:/listMaterialaddresss";
	}

	@RequestMapping("/viewMaterialaddress")
	public ModelAndView viewMaterialaddress(@RequestParam Integer materialaddressId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("materialaddress", materialaddressDAO.findMaterialaddressByPrimaryKey(materialaddressId));
		mav.setViewName("materialaddress/viewMaterialaddress");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteMaterialaddresss")
	public ModelAndView confirmDeleteMaterialaddresss(
			@RequestParam(value = "deleteMaterialaddresssIds", required = true) Set<Integer> deleteMaterialaddresssIds) {
		ModelAndView mav = new ModelAndView();

		Set<Materialaddress> deleteMaterialaddresss = materialaddressDAO
				.findMaterialaddresssByIds(deleteMaterialaddresssIds);
		Set<Materialaddress> referencedMaterialaddresss = materialaddressService
				.loadReferenceMaterialaddresss(deleteMaterialaddresss);
		boolean hasReferenced;
		if (referencedMaterialaddresss.size() == 0) {
			mav.addObject("deleteEntitys", deleteMaterialaddresss);
			hasReferenced = false;
		} else {
			deleteMaterialaddresss.removeAll(referencedMaterialaddresss);
			mav.addObject("referencedEntitys", referencedMaterialaddresss);
			mav.addObject("deleteEntitys", deleteMaterialaddresss);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Materialaddress");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listMaterialaddresss");
		return mav;
	}

	@RequestMapping("/deleteMaterialaddress")
	public String deleteMaterialaddress(@RequestParam Integer materialaddressId) {
		Materialaddress materialaddress = materialaddressDAO.findMaterialaddressByPrimaryKey(materialaddressId);
		materialaddressService.deleteMaterialaddress(materialaddress);
		return "forward:/listMaterialaddresss";
	}

	@RequestMapping("/confirmDeleteMaterialaddress")
	public ModelAndView confirmDeleteMaterialaddress(@RequestParam Integer materialaddressId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("materialaddress", materialaddressDAO.findMaterialaddressByPrimaryKey(materialaddressId));

		mav.setViewName("materialaddress/deleteMaterialaddress");

		return mav;
	}

	@RequestMapping("/deleteMaterialaddresss")
	public String deleteMaterialaddresss(
			@RequestParam(value = "deleteMaterialaddresssId", required = true) Set<Integer> deleteMaterialaddresssId) {
		materialaddressService.deleteMaterialaddresss(deleteMaterialaddresssId);
		return "forward:/listMaterialaddresss";
	}

	@RequestMapping("/editMaterialaddress")
	public ModelAndView editMaterialaddress(@RequestParam Integer materialaddressId) {

		ModelAndView mav = new ModelAndView();

		Materialaddress materialaddress = materialaddressDAO.findMaterialaddressByPrimaryKey(materialaddressId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("materialaddress", materialaddress);

		// 把所有的引用materialaddress对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(materialaddressService.loadMaterialaddressObjectReferencedSet(materialaddress));

		// 将编辑页面materialaddress添加到mav中
		mav.setViewName("materialaddress/editMaterialaddress");
		return mav;
	}

	@RequestMapping("/newMaterialaddress")
	public ModelAndView newMaterialaddress() {
		ModelAndView mav = new ModelAndView();
		Materialaddress materialaddress = new Materialaddress();

		mav.addObject("materialaddress", materialaddress);

		// 把所有的引用materialaddress对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(materialaddressService.loadMaterialaddressObjectReferencedSet(materialaddress));

		mav.addObject("newFlag", true);
		mav.setViewName("materialaddress/editMaterialaddress");

		return mav;
	}

	@RequestMapping("/saveMaterialaddress")
	public ModelAndView saveMaterialaddress(@ModelAttribute Materialaddress materialaddress) {

		ModelAndView mav = new ModelAndView();

		Address addressTemp = materialaddress.getAddress();
		Address address = addressTemp == null ? null
				: CmiHibernateProxyUtil.deproxy(addressDAO.findAddressById(addressTemp.getId()));
		Material materialTemp = materialaddress.getMaterial();
		Material material = materialTemp == null ? null
				: CmiHibernateProxyUtil.deproxy(materialDAO.findMaterialById(materialTemp.getId()));
		Unit unitTemp = materialaddress.getUnit();
		Unit unit = unitTemp == null ? null : CmiHibernateProxyUtil.deproxy(unitDAO.findUnitById(unitTemp.getId()));

		Materialaddress existingMaterialaddress = CmiHibernateProxyUtil.deproxy(materialaddressDAO
				.findMaterialaddressByPhysicalPrimaryKey(material.getId(), address.getId(), materialaddress.getDate()));

		materialaddress.setAddress(address);
		materialaddress.setMaterial(material);
		materialaddress.setUnit(unit);
		// 新建记录
		if (materialaddress.getId() == null) {
			if (materialaddress.equals(existingMaterialaddress)) {

				mav.addAllObjects(materialaddressService.loadMaterialaddressClassReferencedSet(materialaddress));
				// mav = editContinue(mav, materialaddress);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("materialaddress/editMaterialaddress");
			} else {
				materialaddressService.saveMaterialaddress(materialaddress);
				mav.setViewName("forward:/listMaterialaddresss");
			}
		}
		// 编辑记录
		else {
			Materialaddress originMaterialaddress = CmiHibernateProxyUtil
					.deproxy(materialaddressDAO.findMaterialaddressByPrimaryKey(materialaddress.getId()));
			if (!materialaddress.equals(originMaterialaddress) && materialaddress.equals(existingMaterialaddress)) {
				mav.addAllObjects(materialaddressService.loadMaterialaddressClassReferencedSet(materialaddress));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("materialaddress/editMaterialaddress");
			} else {
				materialaddressService.saveMaterialaddress(materialaddress);
				mav.setViewName("forward:/listMaterialaddresss");
			}
		}
		return mav;
	}

	@RequestMapping("/materialaddressController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
